package com.lx.service.impl;

import com.lx.factory.JwtUserFactory;
import com.lx.model.User;
import com.lx.repository.UserRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author 段誉
 * @create 2019-06-26 13:40
 */
@Service
public class JwtUserDetailsServiceImpl implements UserDetailsService {
  @Autowired
  private UserRepository userRepository;

  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    User user = userRepository.findByUsername(username);
    if (Objects.isNull(user)) {
      throw new UsernameNotFoundException(String.format("No user found with username %s.", username));
    }
    return JwtUserFactory.create(user);
  }
}
